home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / pdcurs22 / src / portable / border.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-26  |  12.0 KB  |  470 lines

  1. /*
  2. ***************************************************************************
  3. * This file comprises part of PDCurses. PDCurses is Public Domain software.
  4. * You may use this code for whatever purposes you desire. This software
  5. * is provided AS IS with NO WARRANTY whatsoever.
  6. * Should this software be used in another application, an acknowledgement
  7. * that PDCurses code is used would be appreciated, but is not mandatory.
  8. *
  9. * Any changes which you make to this software which may improve or enhance
  10. * it, should be forwarded to the current maintainer for the benefit of 
  11. * other users.
  12. *
  13. * The only restriction placed on this code is that no distribution of
  14. * modified PDCurses code be made under the PDCurses name, by anyone
  15. * other than the current maintainer.
  16. * See the file maintain.er for details of the current maintainer.
  17. ***************************************************************************
  18. */
  19. #define    CURSES_LIBRARY    1
  20. #include <curses.h>
  21.  
  22. /* undefine any macros for functions defined in this module */
  23. #undef    border
  24. #undef    wborder
  25. #undef    box
  26. #undef    hline
  27. #undef    whline
  28. #undef    vline
  29. #undef    wvline
  30.  
  31. /* undefine any macros for functions called by this module if in debug mode */
  32. #ifdef PDCDEBUG
  33. #endif
  34.  
  35. #ifdef PDCDEBUG
  36. char *rcsid_border  = "$Id$";
  37. #endif
  38.  
  39. /*man-start*********************************************************************
  40.  
  41.   Name:                                                        border
  42.  
  43.   Synopsis:
  44.       int border(chtype ls, chtype rs, chtype ts, chtype bs, chtype tl, 
  45.                  chtype tr, chtype bl, chtype br);
  46.       int wborder(WINDOW *win, chtype ls, chtype rs, chtype ts, 
  47.                   chtype bs, chtype tl, chtype tr, chtype bl, chtype br);
  48.       int box(WINDOW *win, chtype verch, chtype horch);
  49.       int hline(chtype ch, int n);
  50.       int vline(chtype ch, int n);
  51.       int whline(WINDOW *win, chtype ch, int n);
  52.       int wvline(WINDOW *win, chtype ch, int n);
  53.  
  54.   X/Open Description:
  55.      The border(), wborder(), and box() routines, a border is drawn
  56.      around the edge of the window. If any of the arguments is zero,
  57.      an appropriate default is used. These default values are specified
  58.      in <curses.h>. The arguments and defaults to these functions are:
  59.          ls    left side of border        ACS_VLINE
  60.          rs    right side of border        ACS_VLINE
  61.          ts    top side of border        ACS_HLINE
  62.          bs    bottom side of border        ACS_HLINE
  63.          tl    top left corner of border    ACS_ULCORNER
  64.          tr    top right corner of border    ACS_URCORNER
  65.          bl    bottom left corner of border    ACS_BLCORNER
  66.          br    bottom right corner of border    ACS_BLCORNER
  67.  
  68.      The hline() and whline() functions draw a left to right line
  69.      using ch starting from the current cursor position. The cursor
  70.      position does not change. The line is at most n characters long
  71.      or as many as will fit in the window.
  72.  
  73.      The vline() and wvline() functions draw a top to bottom line
  74.      using ch starting from the current cursor position. The cursor
  75.      position does not change. The line is at most n characters long
  76.      or as many as will fit in the window.
  77.  
  78.      border(), box(), hline(), and vline() are implemented as macros.
  79.  
  80.   X/Open Return Value:
  81.      These functions return OK on success and ERR on error.
  82.  
  83.   X/Open Errors:
  84.      No errors are defined for these functions.
  85.  
  86.   Portability                             X/Open    BSD    SYS V
  87.                                           Dec '88
  88.       border                                -        -      4.0
  89.       wborder                               -        -      4.0
  90.       box                                   Y        Y       Y
  91.       hline                                 -        -      4.0
  92.       whline                                -        -      4.0
  93.       vline                                 -        -      4.0
  94.       wvline                                -        -      4.0
  95.  
  96. **man-end**********************************************************************/
  97.  
  98. /***********************************************************************/
  99. int wborder(WINDOW *win, chtype ls, chtype rs, chtype ts, 
  100.             chtype bs, chtype tl, chtype tr, chtype bl, chtype br)
  101. /***********************************************************************/
  102. {
  103.     int ymax,xmax;
  104.     int ymin,xmin;
  105.     int    i;
  106.     chtype    vattr;
  107.     chtype    hattr;
  108.  
  109. #ifdef PDCDEBUG
  110.     if (trace_on) PDC_debug("wborder() - called\n");
  111. #endif
  112.  
  113.     if (win == (WINDOW *)NULL)
  114.         return( ERR );
  115.     ymax = win->_maxy - 1;
  116.     xmax = win->_maxx - 1;
  117.     ymin = 0;
  118.     xmin = 0;
  119.  
  120.     if ((ts    & A_ATTRIBUTES)    == 0)
  121.        hattr    = win->_attrs;
  122.     else
  123.        if ((ts & A_COLOR) == 0)
  124.           hattr = (ts & A_ATTRIBUTES) | win->_attrs;
  125.        else
  126.           hattr = (ts & A_ATTRIBUTES);
  127.  
  128.     if ((ls    & A_ATTRIBUTES)    == 0)
  129.        vattr    = win->_attrs;
  130.     else
  131.        if ((ls & A_COLOR) == 0)
  132.           vattr = (ls & A_ATTRIBUTES) | win->_attrs;
  133.        else
  134.           vattr = (ls & A_ATTRIBUTES);
  135.  
  136.     ls = (ls == 0) ? ACS_VLINE | vattr : ls;
  137.     rs = (rs == 0) ? ACS_VLINE | vattr : rs;
  138.     ts = (ts == 0) ? ACS_HLINE | hattr : ts;
  139.     bs = (bs == 0) ? ACS_HLINE | hattr : bs;
  140.     tl = (tl == 0) ? ACS_ULCORNER | vattr : tl;
  141.     tr = (tr == 0) ? ACS_URCORNER | vattr : tr;
  142.     bl = (bl == 0) ? ACS_LLCORNER | vattr : bl;
  143.     br = (br == 0) ? ACS_LRCORNER | vattr : br;
  144.  
  145.     ls = (ls & A_ATTRIBUTES) ? ls : ls | vattr;
  146.     rs = (rs & A_ATTRIBUTES) ? rs : rs | vattr;
  147.     ts = (ts & A_ATTRIBUTES) ? ts : ts | hattr;
  148.     bs = (bs & A_ATTRIBUTES) ? bs : bs | hattr;
  149.     tl = (tl & A_ATTRIBUTES) ? tl : tl | vattr;
  150.     tr = (tr & A_ATTRIBUTES) ? tr : tr | vattr;
  151.     bl = (bl & A_ATTRIBUTES) ? bl : bl | vattr;
  152.     br = (br & A_ATTRIBUTES) ? br : br | vattr;
  153.  
  154.     for (i = xmin + 1; i <= xmax - 1; i++)
  155.     {
  156.         win->_y[ymin][i] = ts;
  157.         win->_y[ymax][i] = bs;
  158.     }
  159.  
  160.     for (i = ymin + 1; i <= ymax - 1; i++)
  161.     {
  162.         win->_y[i][xmin] = ls;
  163.         win->_y[i][xmax] = rs;
  164.     }
  165.  
  166.     win->_y[ymin][xmin] = tl;
  167.     win->_y[ymin][xmax] = tr;
  168.     win->_y[ymax][xmin] = bl;
  169.     win->_y[ymax][xmax] = br;
  170.  
  171.     for (i = ymin; i <= ymax; i++)
  172.     {
  173.         if (win->_firstch[i] == _NO_CHANGE)
  174.         {
  175.             win->_firstch[i] = xmin;
  176.             win->_lastch[i] = xmax;
  177.         }
  178.         else
  179.         {
  180.             win->_firstch[i] = min(win->_firstch[i], xmin);
  181.             win->_lastch[i] = max(win->_lastch[i], xmax);
  182.         }
  183.     }
  184.     return (OK);
  185. }
  186. /***********************************************************************/
  187. int border(chtype ls, chtype rs, chtype ts, chtype bs,
  188.            chtype tl, chtype tr, chtype bl, chtype br)
  189. /***********************************************************************/
  190. {
  191.     int ymax,xmax;
  192.     int ymin,xmin;
  193.     int    i;
  194.  
  195. #ifdef PDCDEBUG
  196.     if (trace_on) PDC_debug("border() - called\n");
  197. #endif
  198.  
  199.     if (stdscr == (WINDOW *)NULL)
  200.         return( ERR );
  201.  
  202.     return(wborder(stdscr,ls,rs,ts,bs,tl,tr,bl,br));
  203. }
  204. /***********************************************************************/
  205. int box(WINDOW *win, chtype verch, chtype horch)
  206. /***********************************************************************/
  207. {
  208. #ifdef PDCDEBUG
  209.     if (trace_on) PDC_debug("box() - called\n");
  210. #endif
  211.  
  212.     if (win == (WINDOW *)NULL)
  213.         return( ERR );
  214.  
  215.     return(wborder(win,verch,verch,horch,horch,0,0,0,0));
  216. }
  217. /***********************************************************************/
  218. int    hline(chtype ch, int n)
  219. /***********************************************************************/
  220. {
  221.     chtype    attr;
  222.     int    startpos;
  223.     int    endpos;
  224.  
  225. #ifdef PDCDEBUG
  226.     if (trace_on) PDC_debug("hline() - called\n");
  227. #endif
  228.  
  229.     if (stdscr == (WINDOW *)NULL)
  230.         return( ERR );
  231.  
  232.     if (n < 1)
  233.         return( ERR );
  234.  
  235.     endpos = min(stdscr->_curx + n -1, stdscr->_maxx);
  236.  
  237.     /*
  238.     ** If the incoming character doesn't have it's own attribute
  239.     ** then use the current attributes for the window.
  240.     */
  241.  
  242.     if ((ch & A_ATTRIBUTES) == 0)
  243.         attr = stdscr->_attrs;
  244.     else
  245.         if ((ch & A_COLOR) == 0)
  246.             attr = (ch & A_ATTRIBUTES) | stdscr->_attrs;
  247.         else
  248.             attr = (ch & A_ATTRIBUTES);
  249.  
  250.     ch &= A_CHARTEXT;
  251.  
  252.     /*
  253.     ** If the incoming character is zero then use the default horizontal
  254.     ** line character.
  255.     */
  256.  
  257.     if (ch == 0)
  258.         ch = ACS_HLINE;
  259.  
  260.     ch |= attr;
  261.     startpos = stdscr->_curx;
  262.  
  263.     for (n = stdscr->_curx; n <= endpos; n++)
  264.         stdscr->_y[stdscr->_cury][n] = ch;
  265.  
  266.     n = stdscr->_cury;
  267.  
  268.     if (stdscr->_firstch[n] == _NO_CHANGE)
  269.     {
  270.         stdscr->_firstch[n] = startpos;
  271.         stdscr->_lastch[n] = endpos;
  272.     }
  273.     else
  274.     {
  275.         stdscr->_firstch[n] = min(stdscr->_firstch[n], startpos);
  276.         stdscr->_lastch[n] = max(stdscr->_lastch[n], endpos);
  277.     }
  278.  
  279.     return (OK);
  280. }
  281. /***********************************************************************/
  282. int    whline(WINDOW *win, chtype ch, int n)
  283. /***********************************************************************/
  284. {
  285.     chtype    attr;
  286.     int    startpos;
  287.     int    endpos;
  288.  
  289. #ifdef PDCDEBUG
  290.     if (trace_on) PDC_debug("whline() - called\n");
  291. #endif
  292.  
  293.     if (win == (WINDOW *)NULL)
  294.         return( ERR );
  295.  
  296.     if (n < 1)
  297.         return( ERR );
  298.  
  299.     endpos = min(win->_curx + n -1, win->_maxx);
  300.  
  301.     /*
  302.     ** If the incoming character doesn't have it's own attribute
  303.     ** then use the current attributes for the window.
  304.     */
  305.  
  306.     if ((ch & A_ATTRIBUTES) == 0)
  307.         attr = win->_attrs;
  308.     else
  309.         if ((ch & A_COLOR) == 0)
  310.             attr = (ch & A_ATTRIBUTES) | win->_attrs;
  311.         else
  312.             attr = (ch & A_ATTRIBUTES);
  313.  
  314.     ch &= A_CHARTEXT;
  315.  
  316.     /*
  317.     ** If the incoming character is zero then use the default horizontal
  318.     ** line character.
  319.     */
  320.  
  321.     if (ch == 0)
  322.         ch = ACS_HLINE;
  323.  
  324.     ch |= attr;
  325.     startpos = win->_curx;
  326.  
  327.     for (n = win->_curx; n <= endpos; n++)
  328.         win->_y[win->_cury][n] = ch;
  329.  
  330.     n = win->_cury;
  331.  
  332.     if (win->_firstch[n] == _NO_CHANGE)
  333.     {
  334.         win->_firstch[n] = startpos;
  335.         win->_lastch[n] = endpos;
  336.     }
  337.     else
  338.     {
  339.         win->_firstch[n] = min(win->_firstch[n], startpos);
  340.         win->_lastch[n] = max(win->_lastch[n], endpos);
  341.     }
  342.  
  343.     return (OK);
  344. }
  345. /***********************************************************************/
  346. int    vline(chtype ch, int n)
  347. /***********************************************************************/
  348. {
  349.     chtype    attr;
  350.     int    endpos;
  351.  
  352. #ifdef PDCDEBUG
  353.     if (trace_on) PDC_debug("vline() - called\n");
  354. #endif
  355.  
  356.     if (stdscr == (WINDOW *)NULL)
  357.         return( ERR );
  358.  
  359.     if (n < 1)
  360.         return( ERR );
  361.  
  362.     endpos = min(stdscr->_cury + n -1, stdscr->_maxy);
  363.  
  364.     /*
  365.     ** If the incoming character doesn't have it's own attribute
  366.     ** then use the current attributes for the window.
  367.     */
  368.  
  369.     if ((ch & A_ATTRIBUTES) == 0)
  370.         attr = stdscr->_attrs;
  371.     else
  372.         if ((ch & A_COLOR) == 0)
  373.             attr = (ch & A_ATTRIBUTES) | stdscr->_attrs;
  374.         else
  375.             attr = (ch & A_ATTRIBUTES);
  376.  
  377.     ch &= A_CHARTEXT;
  378.  
  379.     /*
  380.     ** If the incoming character is zero then use the default vertical
  381.     ** line character.
  382.     */
  383.  
  384.     if (ch == 0)
  385.         ch = ACS_VLINE;
  386.  
  387.     ch |= attr;
  388.  
  389.     for (n = stdscr->_cury; n <= endpos; n++)
  390.     {
  391.         stdscr->_y[n][stdscr->_curx] = ch;
  392.  
  393.         if (stdscr->_firstch[n] == _NO_CHANGE)
  394.         {
  395.             stdscr->_firstch[n] = stdscr->_curx;
  396.             stdscr->_lastch[n] = stdscr->_curx;
  397.         }
  398.         else
  399.         {
  400.             stdscr->_firstch[n] = min(stdscr->_firstch[n], stdscr->_curx);
  401.             stdscr->_lastch[n] = max(stdscr->_lastch[n], stdscr->_curx);
  402.         }
  403.     }
  404.  
  405.     return (OK);
  406. }
  407. /***********************************************************************/
  408. int    wvline(WINDOW *win, chtype ch, int n)
  409. /***********************************************************************/
  410. {
  411.     chtype    attr;
  412.     int    endpos;
  413.  
  414. #ifdef PDCDEBUG
  415.     if (trace_on) PDC_debug("wvline() - called\n");
  416. #endif
  417.  
  418.     if (win == (WINDOW *)NULL)
  419.         return( ERR );
  420.  
  421.     if (n < 1)
  422.         return( ERR );
  423.  
  424.     endpos = min(win->_cury + n -1, win->_maxy);
  425.  
  426.     /*
  427.     ** If the incoming character doesn't have it's own attribute
  428.     ** then use the current attributes for the window.
  429.     */
  430.  
  431.     if ((ch & A_ATTRIBUTES) == 0)
  432.         attr = win->_attrs;
  433.     else
  434.         if ((ch & A_COLOR) == 0)
  435.             attr = (ch & A_ATTRIBUTES) | win->_attrs;
  436.         else
  437.             attr = (ch & A_ATTRIBUTES);
  438.  
  439.     ch &= A_CHARTEXT;
  440.  
  441.     /*
  442.     ** If the incoming character is zero then use the default vertical
  443.     ** line character.
  444.     */
  445.  
  446.     if (ch == 0)
  447.         ch = ACS_VLINE;
  448.  
  449.     ch |= attr;
  450.  
  451.     for (n = win->_cury; n <= endpos; n++)
  452.     {
  453.         win->_y[n][win->_curx] = ch;
  454.  
  455.         if (win->_firstch[n] == _NO_CHANGE)
  456.         {
  457.             win->_firstch[n] = win->_curx;
  458.             win->_lastch[n] = win->_curx;
  459.         }
  460.         else
  461.         {
  462.             win->_firstch[n] = min(win->_firstch[n], win->_curx);
  463.             win->_lastch[n] = max(win->_lastch[n], win->_curx);
  464.         }
  465.     }
  466.  
  467.     return (OK);
  468. }
  469.